﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using timetableasy.Models;

namespace timetableasy.Controllers
{
    public class EventsController : Controller
    {
        EventRepository eventRepository = new EventRepository();
        //
        // GET: /Events/

        public ActionResult Index()
        {
            var events = eventRepository.FindAllEvents().ToList();
            return View("Index", events);
        }

        //
        // GET: /Events/Details/5

        public ActionResult Details(int id)
        {
            Event e = eventRepository.GetEventById(id);
            if (e == null)
                return View("NotFound");
            else
                return View("Details", e);
        }

        //
        // GET: /Events/Create

        public ActionResult Create()
        {
            Event e = new Event()
            {
                EventName = string.Empty,
                ClassRoom = new ClassRoom(),
                CourseProgram = new CourseProgram(),
                EndDate = new DateTime(),
                EventAudiance = new EventAudiance(),
                EventType = new EventType(),
                IsMandatoryToDisplay = false,
                SpaceName = string.Empty,
                SpeakerName = string.Empty,
                StartDate = new DateTime(),
                User = new User()
            };

            return View(e);
        }

        //
        // POST: /Events/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(Event e)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    eventRepository.Add(e);
                    eventRepository.Save();

                    return RedirectToAction("Details", new { id = e.EventId });
                }
                catch
                {
                    foreach (var issue in e.GetRuleViolations())
                    {
                        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                    }
                }
            }

            return View(e);
        }

        //
        // GET: /Events/Edit/5

        public ActionResult Edit(int id)
        {
            Event e = eventRepository.GetEventById(id);
            if (e != null)
            {
                return View(e);
            }
            else
            {
                return RedirectToAction("Index");
            }

        }

        //
        // POST: /Events/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Event e = eventRepository.GetEventById(id);
            if (e == null)
                return View("NotFound");
            try
            {
                UpdateModel(e);
                eventRepository.Save();
                return RedirectToAction("Details", new { id = e.EventId });
            }
            catch
            {
                foreach (var issue in e.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                return View(e);
            }
        }

        //
        // GET: /Events/Delete/5

        public ActionResult Delete(int id)
        {
            Event e = eventRepository.GetEventById(id);

            if (e == null)
                return View("NotFound");

            //if (!event.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            return View(e);
        }

        // 
        // HTTP POST: /Dinners/Delete/1

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            Event e = eventRepository.GetEventById(id);

            if (e == null)
                return View("NotFound");

            //if (!event.IsHostedBy(User.Identity.Name))
            //    return View("InvalidOwner");

            eventRepository.Delete(e);
            eventRepository.Save();

            return View("Deleted");
        }
    }
}
